import json
import sys
import time
from xftinterfaces.utils.SM2 import *
from xftinterfaces.utils.SM3 import *
import requests


class XftInterface:
    def __init__(self):
        # self.host = "https://api.cmburl.cn:8065"
        # self.privateKey = "37b9916dfd913ca5cd2043d378a55e90000a9a06da8f8bb9ea43ec5305c1698c"
        # self.publicKey = "8e6a77398859de1ad0d4828610d7e2f3df2d8c3d52c7b7d1187cfd9164efadb4da5c1b434b53a58d9ab976425311a997546636b0654f41df26dda6a648a7c0a0"
        # self.CSCAPPUID = "7dec742c-b2ee-49a0-9b41-a832e8d0bfe7"
        # self.CSCPRJCOD = "XFB14001"
        # self.CSCUSRNBR = "A0001"
        # self.CSCUSRUID = "AUTO0001"
        # self.appid = "7dec742c-b2ee-49a0-9b41-a832e8d0bfe7"

        # 世纪正式环境
        self.host = "https://api.cmbchina.com"
        self.privateKey = "61d28e5cf537841a1b2013679ec4a1eee7d3b423f9a9d49e65a784fbc54ca099"
        self.publicKey = "c229b8faa982e9ad0ba8b47a8df8ebfc65da8953632ba6db8c7fdb56644bc114f88e783b90cc5cf6579d9020f0ce743bd7179eff8ed5c0dd6110e3b564389670"
        self.CSCAPPUID = "37de2437-43b7-4298-a025-ad9499a1894c"
        self.CSCPRJCOD = "XFB27598"
        self.CSCUSRNBR = "A0001"
        self.CSCUSRUID = "AUTO0001"
        self.appid = "37de2437-43b7-4298-a025-ad9499a1894c"
        # 悦达正式环境
        # self.host = "https://api.cmbchina.com"
        # self.privateKey = "00a459fc3cee7bcb18a842de6347bec574b32c95450844d84fc61d4d00ef03e827"
        # self.publicKey = "f73605c458940c04f2892db504b210637a3a9b68f3552c3c7a9ab571b5d16c969f3dbd5f6a1f22b12a942c7abe9b9ed73606ae8277e544f3afbad92a002c897b"
        # self.CSCAPPUID = "0ed56d2d-6146-4e5b-a5e4-4dc38bd83f88"
        # self.CSCPRJCOD = "XFA16430"
        # self.CSCUSRNBR = "A0001"
        # self.CSCUSRUID = "AUTO0001"
        # self.appid = "0ed56d2d-6146-4e5b-a5e4-4dc38bd83f88"

        self.xalbverify = "sm3withsm2"
        self.ContentType = "application/json"
        self.empInfoBodyStr = {}
        self.taxCalcCreateObj = {}
        self.taxCalcDeleteObj = {}
        self.taxcalcsearchObj = {}
        self.taxsearchObj = {}

    def getHttpStatus(arg):
        try:
            codes = arg.status_code
            return codes
        except:
            sys.exit(0)

    def getHttpMessage(arg):
        try:
            reason = arg.reason
            return reason
        except:
            sys.exit(0)

    def postFunction(self, bodyStr, path):
        timestamp = str(int(time.time()))
        path = path + "?CSCAPPUID=" + self.CSCAPPUID + "&CSCPRJCOD=" + self.CSCPRJCOD + "&CSCREQTIM=" + timestamp + "&CSCUSRNBR=" + self.CSCUSRNBR + "&CSCUSRUID=" + self.CSCUSRUID
        url = self.host + path
        sm2_test = SM2(self.privateKey, self.publicKey)
        digest = Hash_sm3(json.dumps(bodyStr), 0)
        method = "POST"
        # 对appid等参数进行拼接
        temp = ' '
        changeLine = '\n'
        includeDigest = 'x-alb-digest: '
        includeTime = 'x-alb-timestamp: '
        signStr = method + temp + path + changeLine + includeDigest + json.dumps(bodyStr) + changeLine + includeTime + timestamp
        apisign = sm2_test.test_SM2_SM3(signStr.encode('utf-8'))
        header = {
            "Content-Type": "application/json;charset=UTF-8",
            "Connection": "close",
            "appid": self.appid,
            "apisign": apisign,
            "x-alb-verify": self.xalbverify,
            "x-alb-digest": digest,
            "x-alb-timestamp": timestamp
        }
        values_json = json.dumps(bodyStr)
        res = requests.post(headers=header, url=url, data=values_json)
        result = json.loads(res.text)
        return result

    def employeeinfosearch(self):
        '''
        员工信息查询
        :return: 返回人员列表
        '''
        # path = "/itax/itaxuat/tax/v2/TXZCBSRY"
        path = "/xft/itax/tax/v2/TXZCBSRY"
        result = self.postFunction(self.empInfoBodyStr, path)
        if "TXSTFQRYZ" in result:
            # if result["SYCOMRETZ"][0]["ERRCOD"] == "0000000":
            return result
        else:
            # 报错记录到表中
            return False

    def taxcalccreate(self):
        '''
                算税批次创建
                :return: 返回人员列表
                '''

        path = "/itax/itaxuat/tax/v2/TXCUDBAT"
        path = "/xft/itax/tax/v2/TXCUDBAT"
        result = self.postFunction(self.taxCalcCreateObj, path)
        return result

    def taxcalcdelete(self):
        '''
        算税批次删除
        :return:
        '''
        # path = "/itax/itaxuat/tax/v2/TXDLTBAT"
        path = "/xft/itax/tax/v2/TXDLTBAT"
        result = self.postFunction(self.taxCalcDeleteObj, path)
        print(result)

    def taxcalcsearch(self):
        '''
        算税批次查询
        :return:
        '''
        # path = "/itax/itaxuat/tax/v2/TXQRYBLS"
        path = "/xft/itax/tax/v2/TXQRYBLS"
        result = self.postFunction(self.taxcalcsearchObj, path)
        print(result)

    def taxsearch(self):
        '''
            核算明细结果查询
                :return: 返回人员列表
                '''
        # path = "/itax/itaxuat/tax/v2/TXCALINF"
        path = "/xft/itax/tax/v2/TXCALINF"
        result = self.postFunction(self.taxsearchObj, path)
        return result
